package rosengarden.gui;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

public class Preview extends JPanel {

	public Preview() {
		
	}
	
	public void setImage(Image i){
		img = i;
		repaint();
	}
	
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		if(img!=null){
			//g.drawImage(img, getInsets().left, getInsets().top, getWidth()-getInsets().left-getInsets().right, getHeight()-getInsets().top-getInsets().bottom, this);
                     if(img.getWidth(this)*getHeight()/img.getHeight(this) < getWidth()-getInsets().right-getInsets().left){
                         g.drawImage(img, (getWidth()-img.getWidth(this)*getHeight()/img.getHeight(this))/2, getInsets().top, img.getWidth(this)*getHeight()/img.getHeight(this), getHeight()-getInsets().top-getInsets().bottom, this);
                     }else{
                         g.drawImage(img, getInsets().left, (getHeight()-(img.getHeight(this)*getWidth()/img.getWidth(this)-getInsets().top*2-getInsets().bottom*1))/2, getWidth()-getInsets().left-getInsets().right, img.getHeight(this)*getWidth()/img.getWidth(this)-getInsets().top-getInsets().bottom*2, this);
                     }

		}
	}
	
	private Image img = null;
}
